﻿#include <QDir>
#include <QFile>
#include <QDebug>
#include <QScreen>
#include <QPixmap>
#include <QDateTime>
#include <QFileInfoList>
#include <QProcess>
#include "JsonRpc/CmdRouter.h"
#include "websocket/websocketmanager.h"
#include "websocket/HttpClientConnection.h"
#include "base/TraceLog.h"


CmdRouter::~CmdRouter()
{
     LogINFO("%s",__FUNCTION__);
}

CmdRouter::CmdRouter(HttpClientConnection * _Connection)
    :AbstractServerHandle<CmdRouter>()
    ,m_Connection(_Connection)
{
    LogDEBUG("%s",__FUNCTION__);

    // No return ,OR return later need other operation to finsished:: eg ,xmpp ,sip ..
    this->bindAndAddJsonMethod("reLogin",&CmdRouter::reLogin);
    this->bindAndAddJsonMethod("DoPullStreamwork",&CmdRouter::doPullStream);
    this->bindAndAddJsonMethod("DoStartPullStream",&CmdRouter::DoStartPullStream);
    this->bindAndAddJsonMethod("DoStopPullStream",&CmdRouter::DoStopPullStream);


}

CmdRouter::CmdRouter()
    :AbstractServerHandle<CmdRouter>()
{
    // No return ,OR return later need other operation to finsished:: eg ,xmpp ,sip ..
    this->bindAndAddJsonMethod("reLogin",&CmdRouter::reLogin);
    this->bindAndAddJsonMethod("DoPullStreamwork",&CmdRouter::doPullStream);
    this->bindAndAddJsonMethod("DoStartPullStream",&CmdRouter::DoStartPullStream);
    this->bindAndAddJsonMethod("DoStopPullStream",&CmdRouter::DoStopPullStream);

}



void CmdRouter::sendJsonString(QJsonObject jsonRoot)
{
    QJsonDocument jsonDocument;
    jsonDocument.setObject(jsonRoot);
    QByteArray byte_array = jsonDocument.toJson(QJsonDocument::Compact);
    QString json_str(byte_array);
    qDebug()<<"json_str"<<json_str;
    //m_pConnector->getWebSocketServer()->sendJson(json_str);
}

void CmdRouter::doPullStream(QJsonDocument &jdc, QString &strOut)
{
    QJsonObject jsonObj = jdc.object();
    strOut = jsonObj.value("params").toString();
    qDebug()<<"params"<<strOut;
}

void CmdRouter::DoStartPullStream(QJsonDocument &jdc, QString &strOut)
{
    int ret = 0;
    QJsonObject jsonObj = jdc.object();

    QJsonObject jsonParams = jsonObj.take("params").toObject();

    QString PullUrl=jsonParams.value("pullurl").toString();
    QString PullUrlnum=jsonParams.value("pullurlnum").toString();
    int num=PullUrlnum.toInt();


    LogDEBUG("%s:PullUrl:%s num:%d", __FUNCTION__,PullUrl.toStdString().c_str(),num);

    if(m_Connection)
    {
        ret = m_Connection->DoStartPullStream(PullUrl, num);
    }

    if(ret < 0)
    {
        if(m_Connection)
        {
            QJsonObject json;
            json.insert("jsonrpc", "2.0");
            json.insert("method", "DoStartPullStream");


                QJsonObject json_1;

                json_1.insert("reason","DoStart error");
                json_1.insert("ret",-1);
                json.insert("params", json_1);

                QJsonDocument document;
                document.setObject(json);
                QByteArray byte_array = document.toJson(QJsonDocument::Compact);
                m_Connection->sendJson(QString (byte_array));

        }
    }
    else
    {
        if(m_Connection)
        {
            QJsonObject json;
            json.insert("jsonrpc", "2.0");
            json.insert("method", "DoStartPullStream");

            QJsonObject json_1;

            json_1.insert("reason","success");
            json_1.insert("ret",0);
            json.insert("params", json_1);

            QJsonDocument document;
            document.setObject(json);
            QByteArray byte_array = document.toJson(QJsonDocument::Compact);
            m_Connection->sendJson(QString (byte_array));

        }

    }

}


void CmdRouter::DoStopPullStream(QJsonDocument &jdc, QString &strOut)
{
    int ret = 0;
    QJsonObject jsonObj = jdc.object();
    QString PullUrl = jsonObj.value("params").toString();
    LogDEBUG("%s:PullUrl:%s", __FUNCTION__,PullUrl.toStdString().c_str());

    if(m_Connection)
    {
        ret = m_Connection->DoStopPullStream(PullUrl);
    }

    if(ret < 0)
    {
        if(m_Connection)
        {
            QJsonObject json;
            json.insert("jsonrpc", "2.0");
            json.insert("method", "DoStopPullStream");


                QJsonObject json_1;

                json_1.insert("reason","input error");
                json_1.insert("ret",-1);
                json.insert("params", json_1);

                QJsonDocument document;
                document.setObject(json);
                QByteArray byte_array = document.toJson(QJsonDocument::Compact);
                m_Connection->sendJson(QString (byte_array));


        }
    }
    else
    {
        if(m_Connection)
        {
            QJsonObject json;
            json.insert("jsonrpc", "2.0");
            json.insert("method", "DoStopPullStream");

            QJsonObject json_1;

            json_1.insert("reason","success");
            json_1.insert("ret",0);
            json.insert("params", json_1);

            QJsonDocument document;
            document.setObject(json);
            QByteArray byte_array = document.toJson(QJsonDocument::Compact);
            m_Connection->sendJson(QString (byte_array));
        }

    }

}



void CmdRouter::reLogin(QJsonDocument &jdc, QString &strOut)
{
    QJsonObject jsonObj = jdc.object();
    QJsonObject jsonParams = jsonObj.take("params").toObject();
    QString serverIP=jsonParams.value("serverIP").toString();
    QString userName=jsonParams.value("userName").toString();
    QString password=jsonParams.value("password").toString();
    //m_pConnector->getLocalSetting()->setXmppServerIP(serverIP);

    //m_pConnector->getLocalSetting()->setXmppNumber(userName);
    //m_pConnector->getLocalSetting()->setXmppPwd(password);
    //先清楚一下可能存在的冲突.强制登陆.
    //m_pConnector->getXmppClient()->deConflict();
    //m_pConnector->getXmppClient()->changeServer(userName,password,serverIP);
}
